package com.feibi.study.Dialog

import android.app.Dialog
import android.content.Context
import android.view.*
import android.widget.FrameLayout
import com.feibi.study.R


class BottomDialog(private val context: Context, view: View) {
    private var dialog: Dialog? = null
    private val display: Display

    init {
        val windowManager = context
            .getSystemService(Context.WINDOW_SERVICE) as WindowManager
        display = windowManager.defaultDisplay
        builder(view)
        setCancelable(true)
    }

    fun builder(view: View): BottomDialog {
        dialog = Dialog(context, R.style.AlertDialogStyle)
        dialog!!.setContentView(view)
        view.findViewById<View>(R.id.rootLayout).layoutParams = FrameLayout.LayoutParams(
            (display.width), ViewGroup.LayoutParams.WRAP_CONTENT
        )
        val window = dialog?.window
        window?.setGravity(Gravity.BOTTOM)
        window?.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        window?.setWindowAnimations(R.style.dialog_anim)
        return this
    }

    /**
     * 设置点击外部是否消失
     *
     * @param cancel
     * @return
     */
    fun setCancelable(cancel: Boolean): BottomDialog {
        dialog!!.setCancelable(cancel)
        return this
    }

    fun show() {
        dialog!!.show()
    }

    val isShowing: Boolean
        get() = if (dialog != null) {
            if (dialog!!.isShowing) true else false
        } else false

    fun dismiss() {
        if (dialog != null) {
            dialog!!.dismiss()
        }
    }

}


